package com.easywan.core;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class EasyWanDES {
	/**
	 * 将pwd进行加密
	 * @param pwd
	 * @return
	 * 如果密码为：tjoker1234
	 * 加密后：
	 * 加密过程：
	 * t>f 则 - t-f = 8 则为 FE
	 * j>f 则 j-f = 4 则为F4
	 * o>f则 o-f = 9 则 F9
	 * k>f 则 k-f = 5 则 F5
	 * e<f 则 e-f = -1 则 H1
	 * r>f 则 F2
	 * 1<f 则 f-1 = HE
	 * 2<f HD
	 * 4<f HC
	 * FEF4F9F5H1F2HEHDHC
	 * 加上标号  a-z
	 * aFbEcFd4eFh9iFj5kHl1mFn2oHpEqHrDsHtC
	 */
	public static String DES(String pwd){
		String code = pwd.toLowerCase();
		char[] codeChar = code.toCharArray();
		StringBuffer sbResult = new StringBuffer();
		
		for(int i = 0; i <codeChar.length;i++){
			char ch = codeChar[i];
			//sbResult.append((char)('a'+i) +"");
			if(ch <= '9' && ch >= '0'){
				int instance =  -(ch - '0');
				sbResult.append("H" + (char)('F'+instance));
			}else{
				int instance =  ch - 'f';
				if(instance > 0 & instance < 10){
					sbResult.append("F"+instance);
				}else if(instance >= 10){
					sbResult.append("F"+(char)('A'+instance-10));
				}else{
					sbResult.append("H" + ((char)(-instance+'0')));
				}
			}
		}
		return sbResult.toString();
	}
	public static String enDES(String pwd){
		if("".equals(pwd)){
			return "";
		}
		StringBuffer sbResult = new StringBuffer();
		String tempPwd = pwd;
		while(tempPwd.length() > 0){
			String subPwd = tempPwd.substring(0, 2);
			tempPwd = tempPwd.substring(2);
			char ch1 = subPwd.charAt(0);
			char ch2 = subPwd.charAt(1);
			//System.out.println(subPwd);
			if(ch2 >= '0' && ch2 <= '9'){
				if(ch1 == 'F'){
					sbResult.append((char)(ch2-'0' + 'F'));
				}else{
					sbResult.append((char)(0 - ((ch2 - '0')-'F')));
				}
			}else
				if(ch1 == 'F'){
					sbResult.append((char)(ch2-'A' + 'P'));
				}else{
					sbResult.append(0 - (ch2 - 'F'));
				}
		}
		return sbResult.toString().toLowerCase();
	}
	public static String videoDES(String videoString,String pwd){
		StringBuffer sbResult  = new StringBuffer();
		int vl= videoString.length() / 1000;
		String endVideo = videoString.substring(vl);
		System.out.println("压缩长度："+vl);
		videoString = videoString.substring(0,vl);
		int l = pwd.length();
		while(videoString.length() > 0){
			System.out.println("压缩进度："+((vl - videoString.length())*1.0/vl));
			if(videoString.length() < l){
				sbResult.append(videoString);
				break;
			}
			String videoStringTemp = videoString.substring(0,l);
			System.out.println("正在加密数据:"+videoStringTemp);
			videoString = videoString.substring(l);
				for(int i = 0; i < l-1; i++){
					short a = (short) videoStringTemp.charAt(i);
					short b = (short) pwd.charAt(i);
					short c = (short) (a^b);
					//System.out.println(a+"^"+b+"="+c);
					sbResult.append((char)c);
				}
				short a = (short) videoStringTemp.charAt(l-1);
				short b = (short) pwd.charAt(l-1);
				short c = (short) (a^b);
				sbResult.append((char)c);
		}
		sbResult.append(endVideo);
		return sbResult.toString();
	}
	public static String addHeader(String videoString,String pwd,String type) throws NoSuchAlgorithmException{
		StringBuffer sbResult  = new StringBuffer();
		//BASE64Encoder base64en = new BASE64Encoder();
		sbResult.append("FORMAT-"+type+"|");
		MessageDigest md = MessageDigest.getInstance("MD5");
		String md5Pwd =  md.digest(pwd.getBytes()).toString();
		sbResult.append("PASSWORD-"+md5Pwd+"|");
		return sbResult.toString();
	}
}
